package com.fescoservice.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fescoservice.config.PropertiesConfig;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Company;
import com.fescoservice.service.CompanyService;

@Controller
@RequestMapping("/action/Company.htm")
@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "CompanyController"), name = "CompanyAjaxController")   

public class CompanyController {
	private static final Logger log = LogManager.getLogger(CompanyController.class);
	
	@Autowired
	private CompanyService companyService;	

	
	
	

	@RequestMapping(params = "cmd=list")
	public String listCategory(String page,ModelMap map, HttpServletRequest request) throws FSException{
		String compName = (String)request.getParameter("compNameSel");
		log.debug("~~~~~~~~Controller begin listCompany~~~~~~~~"+compName);

		int pageNumber = Integer.parseInt((null==page || "".equals(page))?"1":page);
		String language = request.getSession().getAttribute("userLanguage")==""?"Chinese":(String)request.getSession().getAttribute("userLanguage");
		getListModel(map,compName,language,pageNumber);
		//
		request.setAttribute("compNameSel", compName);
		return "manage/companylist";
	}
	
	private ModelMap getListModel(ModelMap map, String compName,String language,int pageNumber) throws FSException{	
		
		if(null==language || "".equals(language.trim())){
			throw new FSException("FS_ERROR_SESSIONOUT","");
		}
		int first = PropertiesConfig.getInstance().getInt("ListSize.Company")*(pageNumber-1);
		Integer counts  = companyService.countCompanies(compName);
		List list = companyService.findCompanyByName(compName,first);
		map.addAttribute("companylist", list);
		map.addAttribute("counts", counts);
		map.addAttribute("pageNumber", pageNumber);
		return map;
	}
	
	
	
	@RequestMapping(params = "cmd=addCompanyInit")
	public String addInit(ModelMap map,HttpServletRequest request) throws FSException{	
		String compName = (String)request.getParameter("compNameSel");
		request.setAttribute("compNameSel", compName);
		return "manage/addCompany";
	}
	
	@RequestMapping(params = "cmd=addCompany")
	public String addCompany(Company company, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = (String)request.getSession().getAttribute("userLanguage");
		companyService.saveCompany(company);
		String compName = (String)request.getParameter("compNameSel");
		request.setAttribute("compNameSel", compName);
		
		getListModel(map,compName,language,1);
		return "manage/companylist";
	}
	
	@RequestMapping(params = "cmd=modifyCompanyInit")
	public String modifyInit(ModelMap map,HttpServletRequest request) throws FSException{	
		System.out.println("==============================modifyCompanyInit===================================L79-================");
		String id = (String)request.getParameter("compId");
		String compName = (String)request.getParameter("compNameSel");
		request.setAttribute("compNameSel", compName);
		
		Company comp = companyService.findCompanyById(id);

		map.addAttribute("Company", comp);
		return "manage/modifyCompany";
	}
	
	
	@RequestMapping(params = "cmd=modifyCompany")
	public String modifyCompany(Company company, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = (String)request.getSession().getAttribute("userLanguage");
		companyService.updateCompany(company);
		String compName = (String)request.getParameter("compNameSel");
		request.setAttribute("compNameSel", compName);
		getListModel(map,compName,language,1);
		return "manage/companylist";
	}
	
	@RequestMapping(params = "cmd=viewCompany")
	public String viewCompanyInit(ModelMap map,HttpServletRequest request) throws FSException{	
		System.out.println("==============================viewCompany===================================L79-================");
		String id = (String)request.getParameter("compId");
		String compName = (String)request.getParameter("compNameSel");
		request.setAttribute("compNameSel", compName);
		
		Company comp = companyService.findCompanyById(id);

		map.addAttribute("Company", comp);
		return "manage/viewCompany";
	}
	
	
	
	@RequestMapping(params = "cmd=deleteCompany")
	public String deleteCompany(Company company, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = (String)request.getSession().getAttribute("userLanguage");
		companyService.deleteCompany(company);
		String compName = (String)request.getParameter("compNameSel");
		request.setAttribute("compNameSel", compName);
		getListModel(map,compName,language,1);
		return "manage/companylist";
	}

}
